home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter6 / editlabl.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  11KB  |  314 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "editlabl.h"  
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "App";
  20. LPCTSTR lpszTitle   = "ListView_EditLabel()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106.  
  107. LPCTSTR lpszData[4]  = { "Circle", "Rectangle", "Cross", "Check" };
  108. LPCTSTR lpszColor[4] = { "Yellow", "Red",       "Black", "Black" };
  109.  
  110. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  111. {
  112. static HWND  hList    = NULL;
  113. static int   nCurView = IDM_VIEWLARGE;
  114.  
  115.    switch( uMsg )
  116.    {
  117.       case WM_CREATE  :
  118.               {
  119.                  HIMAGELIST hImage, hSmall;
  120.  
  121.                  InitCommonControls();
  122.  
  123.                  // Create the image lists and the list view.
  124.                  //..........................................
  125.                  hImage = ImageList_Create( 32, 32, ILC_COLOR4 | ILC_MASK, 4, 1 ); 
  126.                  hSmall = ImageList_Create( 16, 16, ILC_COLOR4 | ILC_MASK, 4, 1 );
  127.                  hList  = CreateWindowEx( 0, WC_LISTVIEW, "",
  128.                                           WS_CHILD | WS_VISIBLE | LVS_ICON | LVS_EDITLABELS,
  129.                                           10, 100, 100, 100,
  130.                                           hWnd,
  131.                                           (HMENU)1,
  132.                                           hInst,
  133.                                           NULL);
  134.  
  135.                  if ( hList && hImage && hSmall )
  136.                  {
  137.                     LV_COLUMN col;
  138.                     LV_ITEM   item;
  139.                     int       i;
  140.  
  141.                     // Set the large and small image lists.
  142.                     //.....................................
  143.                     ListView_SetImageList( hList, hImage, LVSIL_NORMAL );
  144.                     ListView_SetImageList( hList, hSmall, LVSIL_SMALL );
  145.  
  146.                     // Add the images to the image list.
  147.                     //..................................
  148.                     for( i=0; i<4; i++ )
  149.                     {
  150.                        ImageList_AddIcon( hImage, LoadIcon( hInst, lpszData[i] ) );
  151.                        ImageList_AddIcon( hSmall, LoadIcon( hInst, lpszData[i] ) );
  152.                     }
  153.  
  154.                     // Add the columns for the report view.
  155.                     //.....................................
  156.                     col.mask    = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM; 
  157.                     col.fmt     = LVCFMT_LEFT; 
  158.                     col.cx      = 100; 
  159.  
  160.                     col.pszText  = "Name"; 
  161.                     col.iSubItem = 0;
  162.                     ListView_InsertColumn( hList, 0, &col );
  163.                     
  164.                     col.pszText  = "Color"; 
  165.                     col.iSubItem = 1;
  166.                     ListView_InsertColumn( hList, 1, &col );
  167.  
  168.                     // Insert items into the list view.
  169.                     //.................................
  170.                     for( i=0; i<4; i++ )
  171.                     {
  172.                        item.mask     = LVIF_TEXT | LVIF_IMAGE;
  173.                        item.pszText  = (LPTSTR)lpszData[i];
  174.                        item.iItem    = i;
  175.                        item.iSubItem = 0;
  176.                        item.iImage   = i;
  177.                        ListView_InsertItem( hList, &item );
  178.  
  179.                        item.mask     = LVIF_TEXT;
  180.                        item.pszText  = (LPTSTR)lpszColor[i];
  181.                        item.iSubItem = 1;
  182.                        ListView_SetItem( hList, &item );
  183.                     }
  184.                  }
  185.               }
  186.               break;
  187.  
  188.       case WM_SIZE :
  189.               if ( hList )
  190.                  MoveWindow( hList, 0, 0, LOWORD( lParam ), HIWORD( lParam ), FALSE );
  191.  
  192.               if ( wParam == SIZE_RESTORED || wParam == SIZE_MAXIMIZED )
  193.                  ListView_RedrawItems( hList, 0, ListView_GetItemCount( hList ) );
  194.               break;
  195.  
  196.  
  197.       case WM_INITMENU :
  198.               CheckMenuRadioItem( (HMENU)wParam, 
  199.                                   IDM_VIEWLARGE, IDM_VIEWDETAILS, nCurView, MF_BYCOMMAND );
  200.               break; 
  201.  
  202.  
  203.       case WM_NOTIFY :
  204.               if ( wParam == 1 )
  205.               {
  206.                  LV_DISPINFO* dispInfo = (LV_DISPINFO*)lParam;
  207.  
  208.                  switch( dispInfo->hdr.code )
  209.                  {
  210.                     case LVN_BEGINLABELEDIT :
  211.                            {
  212.                               // Limit the text length of the item.
  213.                               //...................................
  214.                               HWND hEdit = ListView_GetEditControl( hList );
  215.  
  216.                               if ( hEdit )
  217.                                  SendMessage( hEdit, EM_LIMITTEXT, 15, 0 );
  218.                            }
  219.                            break;
  220.  
  221.                     case LVN_ENDLABELEDIT :
  222.                            // If the user edited the text, update the item.
  223.                            //..............................................
  224.                            if ( dispInfo->item.pszText )
  225.                            {
  226.                               dispInfo->item.mask = LVIF_TEXT;
  227.                               ListView_SetItem( hList, &dispInfo->item );
  228.                            }
  229.                            break;
  230.                  }
  231.               }
  232.               break;
  233.  
  234.       case WM_COMMAND :
  235.               switch( LOWORD( wParam ) )
  236.               {
  237.                  case IDM_RENAME :
  238.                         {
  239.                            int nItem = -1;
  240.  
  241.                            nItem = ListView_GetNextItem( hList, nItem, 
  242.                                                          LVNI_ALL | LVNI_SELECTED );
  243.  
  244.                            if ( nItem > -1 )
  245.                               ListView_EditLabel( hList, nItem );
  246.                         }
  247.                         break;      
  248.  
  249.                  case IDM_VIEWLARGE   :
  250.                  case IDM_VIEWSMALL   :
  251.                  case IDM_VIEWLIST    :
  252.                  case IDM_VIEWDETAILS :
  253.                         // Change the view of the list view.
  254.                         //..................................
  255.                         SetWindowLong( hList, GWL_STYLE, WS_CHILD | WS_VISIBLE | LVS_EDITLABELS | 
  256.                                        (LOWORD( wParam ) == IDM_VIEWLARGE ? LVS_ICON :
  257.                                         LOWORD( wParam ) == IDM_VIEWSMALL ? LVS_SMALLICON :
  258.                                         LOWORD( wParam ) == IDM_VIEWLIST  ? LVS_LIST :
  259.                                         LVS_REPORT ) );
  260.  
  261.                         nCurView = LOWORD( wParam );
  262.                         break;
  263.  
  264.                  case IDM_ABOUT :
  265.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  266.                         break;
  267.  
  268.                  case IDM_EXIT :
  269.                         DestroyWindow( hWnd );
  270.                         break;
  271.               }
  272.               break;
  273.       
  274.       case WM_DESTROY :
  275.               if ( ListView_GetImageList( hList, LVSIL_NORMAL ) )
  276.                  ImageList_Destroy( ListView_GetImageList( hList, LVSIL_NORMAL ) );
  277.  
  278.               if ( ListView_GetImageList( hList, LVSIL_SMALL ) )
  279.                  ImageList_Destroy( ListView_GetImageList( hList, LVSIL_SMALL ) );
  280.  
  281.               PostQuitMessage(0);
  282.               break;
  283.  
  284.       default :
  285.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  286.    }
  287.  
  288.    return( 0L );
  289. }
  290.  
  291.  
  292. LRESULT CALLBACK About( HWND hDlg,           
  293.                         UINT message,        
  294.                         WPARAM wParam,       
  295.                         LPARAM lParam)
  296. {
  297.    switch (message) 
  298.    {
  299.        case WM_INITDIALOG: 
  300.                return (TRUE);
  301.  
  302.        case WM_COMMAND:                              
  303.                if (   LOWORD(wParam) == IDOK         
  304.                    || LOWORD(wParam) == IDCANCEL)    
  305.                {
  306.                        EndDialog(hDlg, TRUE);        
  307.                        return (TRUE);
  308.                }
  309.                break;
  310.    }
  311.  
  312.    return (FALSE); 
  313. }
  314.